const moment = require('moment')

module.exports = (sequelize, dataTypes) => {
  const Reply = sequelize.define('reply', {
    id: {type: dataTypes.INTEGER(11), primaryKey: true, autoIncrement: true},
    content: {type: dataTypes.TEXT, allowNull: false},
    createdAt: {
      type: dataTypes.DATE,
      defaultValue: dataTypes.NOW,
      get() {
        return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss')
      }
    },
    updatedAt: {
      type: dataTypes.DATE,
      defaultValue: dataTypes.NOW,
      get() {
        return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss')
      }
    }
  }, {timestamps: true})

  Reply.associate = models => {
    Reply.belongsTo(models.user, {
      foreignKey: 'userId',
      target: 'id',
      constraints: false
    })
  }

  return Reply
}